package io.renren.modules.mvs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.mvs.dao.MvsVehicleDao;
import io.renren.modules.mvs.dto.VehicleDto;
import io.renren.modules.mvs.entity.VehicleEntity;
import io.renren.modules.mvs.service.VehicleService;
import io.renren.modules.sys.dao.SysDictDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service
public class VehicleServiceImpl extends ServiceImpl<MvsVehicleDao, VehicleEntity> implements VehicleService {
    @Autowired
    private SysDictDao sysDictDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        long currPage = 1;
        long limit = 10;
        if(params.get(Constant.PAGE) != null){
            currPage = Long.parseLong((String)params.get(Constant.PAGE));
        }
        if(params.get(Constant.LIMIT) != null){
            limit = Long.parseLong((String)params.get(Constant.LIMIT));
        }
        IPage<VehicleDto> page = this.baseMapper.selectPageByCondition(new Page<>(currPage, limit), params);

        return new PageUtils(page);
    }

    @Override
    public List<VehicleEntity> getPlateNumberListByUserId(Integer userId) {
        return lambdaQuery()
                .select(VehicleEntity::getId, VehicleEntity::getPlateNumber)
                .eq(userId != null, VehicleEntity::getUserId, userId)
                .list();
    }

    @Override
    public List<VehicleEntity> getPlateNumberListByKey(String key, int limit) {
        if (StringUtils.isBlank(key)) {
            return Collections.emptyList();
        }
        if (limit > 1000) {
            limit = 1000;
        }
        return lambdaQuery()
                .select(VehicleEntity::getId, VehicleEntity::getPlateNumber)
                .like(VehicleEntity::getPlateNumber, key)
                .last("limit " + limit).list();
    }

}